/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * MathInputPanel.java
 *
 * Created on Jan 3, 2013, 7:52:40 PM
 */

package br.usp.ime.faguilar.guis;

import br.usp.ime.faguilar.data.DStroke;
import br.usp.ime.faguilar.data.TimePoint;
import br.usp.ime.faguilar.graphics.GStroke;
import br.usp.ime.faguilar.segmentation.OrderedStroke;
import java.awt.Graphics;
import java.util.ArrayList;
import java.util.Calendar;

/**
 *
 * @author frank
 */
public class MathInputPanel extends DrawingArea {
//    private ArrayList<DStroke> strokes;
    private DStroke currentStroke;

    /** Creates new form MathInputPanel */
    public MathInputPanel() {
        initComponents();
//        localInitialization();
    }

//    private void localInitialization(){
//        strokes = new ArrayList<DStroke>();
//    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        setBackground(new java.awt.Color(255, 255, 153));
        addMouseListener(new java.awt.event.MouseAdapter() {
            public void mousePressed(java.awt.event.MouseEvent evt) {
                formMousePressed(evt);
            }
            public void mouseReleased(java.awt.event.MouseEvent evt) {
                formMouseReleased(evt);
            }
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                formMouseClicked(evt);
            }
        });
        addMouseMotionListener(new java.awt.event.MouseMotionAdapter() {
            public void mouseDragged(java.awt.event.MouseEvent evt) {
                formMouseDragged(evt);
            }
        });

        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(0, 400, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(0, 300, Short.MAX_VALUE)
        );
    }// </editor-fold>//GEN-END:initComponents

    private void formMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_formMouseClicked
        // TODO add your handling code here:
    }//GEN-LAST:event_formMouseClicked

    private void formMousePressed(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_formMousePressed
        // TODO add your handling code here:
        currentStroke = new OrderedStroke();

        TimePoint newTimePoint = new TimePoint();
        newTimePoint.setLocation(evt.getX(), evt.getY());
        long timeInMiliseconds = Calendar.getInstance().getTimeInMillis();
        newTimePoint.setTimeInMiliseconds(timeInMiliseconds);
        currentStroke.addCheckingBoundingBox(newTimePoint);

        ArrayList<DStroke> strokes = getDrawingObject().getStrokes();
        ((OrderedStroke)currentStroke).setIndex(strokes.size());
        strokes.add(currentStroke);
    }//GEN-LAST:event_formMousePressed

//    @Override
//    public void paintComponent(Graphics g){
//        super.paintComponent(g);
//        for (DStroke dStroke : strokes) {
//            GStroke.drawStroke(dStroke, g);
//        }
//    }

    private void formMouseDragged(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_formMouseDragged
        // TODO add your handling code here:
        TimePoint newTimePoint = new TimePoint();
        newTimePoint.setLocation(evt.getX(), evt.getY());
        long timeInMiliseconds = Calendar.getInstance().getTimeInMillis();
        newTimePoint.setTimeInMiliseconds(timeInMiliseconds);
        currentStroke.addCheckingBoundingBox(newTimePoint);
        this.repaint();
    }//GEN-LAST:event_formMouseDragged

    private void formMouseReleased(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_formMouseReleased
        // TODO add your handling code here:
//        System.out.println(currentStroke);
        ArrayList<DStroke> strokes = getDrawingObject().getStrokes();
        this.repaint();
//        strokes.add(currentStroke);
    }//GEN-LAST:event_formMouseReleased


    // Variables declaration - do not modify//GEN-BEGIN:variables
    // End of variables declaration//GEN-END:variables

}
